import { _decorator, Component, instantiate, Prefab, Node, Layers, Vec3, isValid } from 'cc';
import { WxSdkManager } from '../platform/ad/WxSdkManager';
const { ccclass, property } = _decorator;

@ccclass('BagManager')
export class BagManager extends Component {
  private static instance = null as unknown as BagManager;
  // 球杆的预制件[默认，高端，奢华，完美]
  @property(Prefab)
  DefaultQiuGan: Prefab = null;
  @property(Prefab)
  HighLevelQiuGan: Prefab = null;
  @property(Prefab)
  LuxuryQiuGan: Prefab = null;
  @property(Prefab)
  PerfectQiuGan: Prefab = null;

  // 球桌的预制件[默认，高端，奢华，完美]
  @property(Prefab)
  DefaultTable: Prefab = null;
  @property(Prefab)
  HighLevelTable: Prefab = null;
  @property(Prefab)
  LuxuryTable: Prefab = null;
  @property(Prefab)
  PerfectTable: Prefab = null;

  private mainCamera: Node = null;
  private cameraPos: Vec3 = new Vec3();
  private qiuGanRoot: Node = null;
  private tableRoot: Node = null;
  private roomRoot: Node = null;
  private objectInstance: Node = null;
  private timestamp = 0;

  // 当前一轮击球是否开始
  public static Instance() {
    return BagManager.instance;
  }
  

  protected onEnable(): void {
   // console.log("555555555555555")
    WxSdkManager.toggleCustomCol33Ad(true)
  }

  protected onDisable(): void {
  //  console.log("777777777777777777")
    WxSdkManager.toggleCustomCol33Ad(false)
  }

  onLoad(): void {
    BagManager.instance = this;
    this.qiuGanRoot = this.node.getChildByName('QiuGanRoot');
    this.tableRoot = this.node.getChildByName('TableRoot');
    this.roomRoot = this.node.getChildByName('RoomRoot');
    this.roomRoot.active = false;
    this.mainCamera = this.node.getParent().getChildByName('Main Camera');
    this.objectInstance = instantiate(this.DefaultQiuGan);
    this.showInstance('DefaultQiuGan');
  }
  start() {

  }
  update(deltaTime: number) {
    this.timestamp += 1;
  }

  public showInstance(name: string) {
    this.roomRoot.active = false;
    if (name === 'DefaultQiuGan') {
      if (isValid(this.objectInstance)) this.objectInstance.destroy();
      this.objectInstance = instantiate(this.DefaultQiuGan);
      this.objectInstance.setParent(this.qiuGanRoot);
      this.cameraPos.set(0, 70, 0);
      this.hideAimLine();
    } else if (name === 'HighLevelQiuGan') {
      if (isValid(this.objectInstance)) this.objectInstance.destroy();
      this.objectInstance = instantiate(this.HighLevelQiuGan);
      this.objectInstance.setParent(this.qiuGanRoot);
      this.cameraPos.set(0, 70, 0);
      this.hideAimLine();
    } else if (name === 'LuxuryQiuGan') {
      if (isValid(this.objectInstance)) this.objectInstance.destroy();
      this.objectInstance = instantiate(this.LuxuryQiuGan);
      this.objectInstance.setParent(this.qiuGanRoot);
      this.cameraPos.set(0, 70, 0);
      this.hideAimLine();
    } else if (name === 'PerfectQiuGan') {
      if (isValid(this.objectInstance)) this.objectInstance.destroy();
      this.objectInstance = instantiate(this.PerfectQiuGan);
      this.objectInstance.setParent(this.qiuGanRoot);
      this.cameraPos.set(0, 70, 0);
      this.hideAimLine();
    } else if (name ==='DefaultTable') {
      if (isValid(this.objectInstance)) this.objectInstance.destroy();
      this.objectInstance = instantiate(this.DefaultTable);
      this.objectInstance.setParent(this.tableRoot);
      this.cameraPos.set(0, 70, 0);
    } else if (name === 'HighLevelTable') {
      if (isValid(this.objectInstance)) this.objectInstance.destroy();
      this.objectInstance = instantiate(this.HighLevelTable);
      this.objectInstance.setParent(this.tableRoot);
      this.cameraPos.set(0, 70, 0);
    } else if (name ==='LuxuryTable') {
      if (isValid(this.objectInstance)) this.objectInstance.destroy();
      this.objectInstance = instantiate(this.LuxuryTable);
      this.objectInstance.setParent(this.tableRoot);
      this.cameraPos.set(0, 70, 0);
    } else if (name === 'PerfectTable') {
      if (isValid(this.objectInstance)) this.objectInstance.destroy();
      this.objectInstance = instantiate(this.PerfectTable);
      this.objectInstance.setParent(this.tableRoot);
      this.cameraPos.set(0, 70, 0);
    } else if (name === 'DefaultRoom') {
      if (isValid(this.objectInstance)) this.objectInstance.destroy();
      this.roomRoot.active = true;
    } else if (name === 'HighLevelRoom') {
      if (isValid(this.objectInstance)) this.objectInstance.destroy();
      this.roomRoot.active = true;
    } else if (name === 'LuxuryRoom') {
      if (isValid(this.objectInstance)) this.objectInstance.destroy();
      this.roomRoot.active = true;
    } else if (name ==='PerfectRoom') {
      if (isValid(this.objectInstance)) this.objectInstance.destroy();
      this.roomRoot.active = true;
    }
    this.mainCamera.setPosition(this.cameraPos);
    this.objectInstance.layer = Layers.Enum.UI_3D;
    this.objectInstance.active = true;
  }

  private hideAimLine() {
    this.objectInstance.getChildByName('AimLine').active = false;
    this.objectInstance.getChildByName('ReflectLine').active = false;
    this.objectInstance.getChildByName('RefractLine').active = false;
  }
}